package net.contrend.system.dao;

import java.sql.Types;
import java.util.Arrays;
import java.util.List;

import net.contrend.system.form.MenuForm;
import net.contrend.system.mapper.MenuFormMapper;
import net.contrend.util.SqlUtils;
import net.contrend.util.StringUtils;

import org.springframework.jdbc.core.JdbcTemplate;


public class RoleMenuDao extends BaseDao {

    private String table = "ct_rolemenu";

    public RoleMenuDao() {
    }

    public RoleMenuDao(JdbcTemplate jt) {
        this.jt = jt;
    }

    public int[] insert(String roleId, String[] menuIds) {
        if (StringUtils.isNotBlank(roleId) && null != menuIds) {
            this.deleteByRoleId(roleId);
            int[] ints = new int[menuIds.length];
            for (int i = 0; i < menuIds.length; i++) {
                if (StringUtils.isNotBlank(menuIds[i])) {
                    ints[i] = this.insert(roleId, menuIds[i]);
                }
            }
            return ints;
        }
        return null;
    }

    public int insert(String roleId, String menuId) {
        String columns = "roleid, menuid";
        String sql = SqlUtils.getInsertSql(this.table, columns);
        System.out.println(String.format("\n%1$s\n", sql));

        return this.jt.update(sql, new Object[] { roleId, menuId }, new int[] { Types.VARCHAR,
                Types.VARCHAR });
    }

    public int deleteByRoleId(String roleId) {
        String sql = SqlUtils.getDeleteSql(this.table, "roleid = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[] { roleId }, new int[] { Types.VARCHAR });
    }

    public int deleteByMenuId(String menuId) {
        String sql = SqlUtils.getDeleteSql(this.table, "menuid = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[] { menuId }, new int[] { Types.VARCHAR });
    }

    public int delete(String roleId, String menuid) {
        String sql = SqlUtils.getDeleteSql(this.table, "roleid = ? and menuid = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[] { roleId, menuid }, new int[] { Types.VARCHAR,
                Types.VARCHAR });
    }

    @SuppressWarnings("unchecked")
    public List<MenuForm> getMenus(String roleId) {
        String sql = "" + "select m.* from ct_rolemenu rm "
                + "    left join ct_menu m on m.menuid = rm.menuid " + "where rm.roleid = ? "
                + "order by m.orderno";
        System.out.println(String.format("\n%1$s\n", sql));
        List<MenuForm> menus = this.jt.query(sql, new Object[] { roleId },
                new int[] { Types.VARCHAR }, new MenuFormMapper());

        return menus;
    }

    public List<MenuForm> getMenus(String[] roleIds) {
        return this.getMenus(roleIds, true);
    }

    @SuppressWarnings("unchecked")
    public List<MenuForm> getMenus(String[] roleIds, boolean isValid) {
        if (null == roleIds || 0 >= roleIds.length) {
            return null;
        }
        String[] params = new String[roleIds.length];
        Arrays.fill(params, "?");
        String sql = "" + "select m.* from ct_rolemenu rm "
                + "    left join ct_menu m on m.menuid = rm.menuid "
                + "    left join ct_menu m2 on m2.menuid = m.parentid "
                + "where rm.roleid in (%1$s) %2$s " + "order by m2.orderno, m.orderno";
        sql = String.format(sql, Arrays.toString(params).replaceAll("[\\[\\]]", ""),
                isValid ? "and m.status = '1'" : "");
        System.out.println(String.format("\n%1$s\n", sql));
        int[] types = new int[roleIds.length];
        Arrays.fill(types, Types.VARCHAR);
        List<MenuForm> menus = this.jt.query(sql, roleIds, types, new MenuFormMapper());

        return menus;
    }
}
